//
//  NSString+JsonStr.m
//  AdverseEvents
//
//  Created by 上海嘉礴1 on 16/8/11.
//  Copyright © 2016年 com.gable.www. All rights reserved.
//

#import "NSString+JsonStr.h"

@implementation NSString (JsonStr)

+ (NSString *)stringJsonValue:(id)JsonValue
{
    NSString *string = nil;
    if (JsonValue == [NSNull null]) {
        string = @"";
    }
    else if (JsonValue==nil)
    {
        string = @"";
    }
    else
    {
        
        if ([JsonValue isKindOfClass:[NSString class]])
        {
            string = JsonValue;
        }
        else if ([JsonValue isKindOfClass:[NSNumber class]])
        {
            string = [JsonValue stringValue];
        }
    }
    return string;
}
/**
 *  时间戳转时间
 */
+ (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}
/**
 * 
 * date转string
 */
+ (NSString *)timeWithDate:(NSDate *)aDate
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSString *dateTime = [formatter stringFromDate:aDate];
    
//    [formatter dateFromString:<#(nonnull NSString *)#>];
    return dateTime;
}

/**
 倒计时
 */
+ (NSString *)timeCountDownInfoWithString:(NSTimeInterval)timeIntrval
{
    
    int timeInt = timeIntrval; //时间差
    
//    int year = timeInt / (3600 * 24 * 30 *12);
//    int month = timeInt / (3600 * 24 * 30);
    int day = timeInt / (3600 * 24);
    int hour = timeInt / 3600;
    int minute = timeInt / 60;
    int second = timeInt;
//    if (year > 0) {
//
//        timeInt = timeInt - year * (3600 * 24 * 30 *12);
//        month = timeInt / (3600 * 24 * 30);
//        timeInt = timeInt - month * (3600 * 24 * 30);
//        day = timeInt / (3600 * 24);
//        timeInt = timeInt - day * (3600 * 24);
//        hour = timeInt / 3600;
//        timeInt = timeInt - hour *3600;
//        minute = timeInt / 60;
//        second = timeInt - minute * 60;
//
//        return [NSString stringWithFormat:@"%d年%d月%d天%d时%d分%d秒",year,month,day,hour,minute,second];
//    }else if(month > 0){
//
//        month = timeInt / (3600 * 24 * 30);
//        timeInt = timeInt - month * (3600 * 24 * 30);
//        day = timeInt / (3600 * 24);
//        timeInt = timeInt - day * (3600 * 24);
//        hour = timeInt / 3600;
//        timeInt = timeInt - hour *3600;
//        minute = timeInt / 60;
//        second = timeInt - minute * 60;
//        return [NSString stringWithFormat:@"%d月%d天%d时%d分%d秒",month,day,hour,minute,second];
//    }else
        if(day > 0){
        day = timeInt / (3600 * 24);
        timeInt = timeInt - day * (3600 * 24);
        hour = timeInt / 3600;
        timeInt = timeInt - hour *3600;
        minute = timeInt / 60;
        second = timeInt - minute * 60;
        return [NSString stringWithFormat:@"%d天%d时%d分%d秒",day,hour,minute,second];
    }else if(hour > 0){
        hour = timeInt / 3600;
        timeInt = timeInt - hour *3600;
        minute = timeInt / 60;
        second = timeInt - minute * 60;
        return [NSString stringWithFormat:@"%d时%d分%d秒",hour,minute,second];
    }else if(minute > 0){
        minute = timeInt / 60;
        second = timeInt - minute * 60;
        return [NSString stringWithFormat:@"%d分%d秒",minute,second];
    }else{
        return [NSString stringWithFormat:@"%d秒",second];
    }
    
}

+ (NSString *)changeObToJsonWith:(id)object
{
    return [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
}
+ (NSString *)timeWithStr:(NSString *)aDateStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    formatter.dateFormat = @"yyyy-mm-dd hh:mm:ss";
    NSDate *date = [formatter dateFromString:aDateStr];
    return [NSString timeWithDate:date];
}
+ (NSString *)stringWithStatus:(NSString *)statuStr
{
    
    NSInteger modelStatu = [statuStr integerValue];

    switch (modelStatu) {
//        case -41:
//        {
//            statuStr = @"复审未通过";
//        }
//            break;
//        case -11:
//        {
//            statuStr = @"初审未通过";
//        }
//            break;
//        case 10:
//        {
//            statuStr = @"待初审";
//        }
//            break;
//        case 11:
//        {
//            statuStr = @"初审通过";
//        }
//            break;
        case 20:
        {
            statuStr = @"预售中";
        }
            break;
        case 30:
        {
            statuStr = @"立即投资";
        }
            break;
//        case 40:
//        {
//            statuStr = @"待复审";
//        }
//            break;
//        case 41:
//        {
//            statuStr = @"复审通过";
//        }
//            break;
//        case 50:
//        {
//            statuStr = @"还款中";
//        }
//            break;
//        case 60:
//        {
//            statuStr = @"已完成";
//        }
//            break;
        case 40:
        {
            statuStr = @"已售罄";
        }
            break;
        case 41:
        {
            statuStr = @"已售罄";
        }
            break;
        case 50:
        {
            statuStr = @"已售罄";
        }
            break;
        case 60:
        {
            statuStr = @"已售罄";
        }
            break;
        default:
            break;
    }
    return statuStr;
}
+ (NSString *)stringWithProfit:(NSString *)profit
{
   
    NSString *profitString = [NSString stringWithFormat:@"%.2f",profit.floatValue * 100.0];

    //   最后几位有 0 去0
    while ([profitString hasSuffix:@"0"]) {
        profitString =[profitString substringToIndex:[profitString length] - 1];
    }
    // 最后一位是 . 去掉
    while ([profitString hasSuffix:@"."]) {
        profitString =[profitString substringToIndex:[profitString length] - 1];
    }
    profitString = [NSString stringWithFormat:@"%@%%",profitString];
    return profitString;
}
+ (NSString *)stringWithMoney:(NSString *)moneyStr
{
    NSInteger money = [moneyStr integerValue];
    if (money>9999) {
//        NSMutableString *string = [NSMutableString stringWithString:[NSString stringWithFormat:@"%ld",money]];
//        [string insertString:@"." atIndex:string.length-4];
//        NSString *string2 = [[NSString alloc] initWithString:string];
        
        // 最后几位有 0 去0
//        while ([string2 hasSuffix:@"0"]) {
//            string2 =[string2 substringToIndex:[string2 length] - 1];
//        }
//        // 最后一位是 . 去掉
//        while ([string2 hasSuffix:@"."]) {
//            string2 =[string2 substringToIndex:[string2 length] - 1];
//        }
        
//        return [NSString stringWithFormat:@"%@万元",string2];
        return [NSString stringWithFormat:@"%.2f万元",money/10000.0];
        
    }else{
        return [NSString stringWithFormat:@"%ld元",(long)money];
    }
}
+ (NSString *)stringWithMoneyNoPointAndYuan:(NSString *)moneyStr
{
    NSInteger money = [moneyStr integerValue];
    if (money>9999) {

        return [NSString stringWithFormat:@"%.2f万",money/10000.0];
        
    }else{
        return [NSString stringWithFormat:@"%ld元",(long)money];
    }
}
+ (NSString *)stringWithMoneyNoPoint:(NSString *)moneyStr
{
    NSInteger money = [moneyStr integerValue];
    if (money>9999) {
        return [NSString stringWithFormat:@"%.f万",money/10000.0];
        
    }else{
        return [NSString stringWithFormat:@"%ld元",(long)money];
    }
}

+ (NSString *)stringWithPoint:(NSString *)pointStr
{
        float money = [pointStr floatValue];
//        NSMutableString *string = [NSMutableString stringWithString:[NSString stringWithFormat:@"%ld",money]];
        NSString *string2 = [NSString stringWithFormat:@"%f",money];
        
        // 最后几位有 0 去0
        while ([string2 hasSuffix:@"0"]) {
            string2 =[string2 substringToIndex:[string2 length] - 1];
        }
        // 最后一位是 . 去掉
        while ([string2 hasSuffix:@"."]) {
            string2 =[string2 substringToIndex:[string2 length] - 1];
        }
        
        return string2;
 
//
}
+ (NSAttributedString *)stringWithStrArray:(NSArray<NSString *> *)strArr fontArray:(NSArray<NSNumber *> *)fontArr
{
    NSMutableAttributedString * totalStr = [[NSMutableAttributedString alloc] initWithString:@""];
    for (NSInteger i=0; i < strArr.count; i++ ) {
        
    
        
        [totalStr appendAttributedString:[[NSAttributedString alloc] initWithString:strArr[i] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontArr[i].integerValue],NSForegroundColorAttributeName:Normal_RedColor}]];
    }
    return totalStr;
}
+ (NSString *)stringWithMoneyNoPointNOYuan:(NSString *)moneyStr
{
    if (!moneyStr.length) {
        return @"无限制";
    }
    NSInteger money = [moneyStr integerValue];
    if (money>9999) {
        return [NSString stringWithFormat:@"%.f万",money/10000.0];
        
    }else{
        return moneyStr;
    }
}
+ (NSAttributedString *)stringWithStrArray:(NSArray<NSString *> *)strArr colorArray:(NSArray<UIColor *> *)colorArr
{
    NSMutableAttributedString * totalStr = [[NSMutableAttributedString alloc] initWithString:@""];
    for (NSInteger i=0; i < strArr.count; i++ ) {
        
        
        
        [totalStr appendAttributedString:[[NSAttributedString alloc] initWithString:strArr[i] attributes:@{NSForegroundColorAttributeName:colorArr[i]}]];
    }
    return totalStr;
}
+ (NSAttributedString *)stringWithStrArray:(NSArray<NSString *> *)strArr fontArray:(NSArray<NSNumber *> *)fontArr colorArray:(NSArray<UIColor *> *)colorArr
{
    NSMutableAttributedString * totalStr = [[NSMutableAttributedString alloc] initWithString:@""];
    for (NSInteger i=0; i < strArr.count; i++ ) {
        [totalStr appendAttributedString:[[NSAttributedString alloc] initWithString:strArr[i] attributes:@{NSForegroundColorAttributeName:colorArr[i],NSFontAttributeName:[UIFont systemFontOfSize:fontArr[i].integerValue]}]];
    }
    return totalStr;
}
@end
